package com.ruimei.controller;

import com.alibaba.fastjson.JSONArray;
import com.ruimei.entitys.Task;
import com.ruimei.entitys.Teacher;
import com.ruimei.service.ITaskService;
import com.ruimei.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RestController
@Api(tags={" 作业 "})
@Scope("prototype")
public class  TaskController
{
  @Resource
  private ITaskService task;
  @Resource
  private RedisTemplate<String,String> redisTemplate;

  @ApiOperation("发布作业")
  @PostMapping({"/puttask"})
  public Map<String, Object> putTask(Task entity, HttpServletRequest request)
  {
    Teacher teacher = new Teacher();
    teacher.setId(TokenUtils.getTeacherId(request));
     if(redisTemplate.hasKey(teacher.getId())){
       teacher = JSONArray.parseObject(redisTemplate.opsForValue().get(teacher.getId()),Teacher.class);
    }
    Map<String, Object> map = new HashMap();
    if (this.task.putTask(entity,teacher))
    {
      map.put("status", Boolean.valueOf(true));
      map.put("message", "发布成功");
      return map;
    }
    map.put("status", Boolean.valueOf(true));
    map.put("message", "发布失败");
    
    return map;
  }
  
  @ApiOperation("查看作业")
  @PostMapping({"/taskview"})
  public Map<String, Object> taskView(String className, String taskId)
  {
    return this.task.taskView(className, taskId);
  }
  
  @ApiOperation("查看老师所授科目")
  @GetMapping({"/getSubjectByTeacherId/{id}"})
  public Map<String, Object> getSubjectByTeacherId(@PathVariable String id)
  {
    return this.task.getSubjectByTeacherId(id);
  }

  @ApiOperation("查看老师所授科目")
  @GetMapping({"/getSubject"})
  public Map<String, Object> getSubject(HttpServletRequest request)
  {
    Teacher teacher = new Teacher();
    teacher.setId(TokenUtils.getTeacherId(request));
    return this.task.getSubjectByTeacherId(teacher.getId());
  }

  @ApiOperation("删除作业")
  @PostMapping({"/deletetask"})
  public Map<String, Object> deletetask(Task task,HttpServletRequest request)
  {
    String teacherId = TokenUtils.getTeacherId(request);
    return this.task.deletetask(task,teacherId);
  }


}